<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class AddressController extends Controller
{
	//个人中心地址显示
	public function getIndex()
	{
		//获取登录用户的地址
		$addr = DB::table('shop_address')->where('user_id',session('id'))->get();
		
		// dd($defAddr_id);
		return view('home.address.index',['addr'=>$addr]);
	}

	//修改地址页面
	public function getEdit(Request $request)
	{
		// dd($request->input('address_id'));
		//获取地址信息
		//获取登录用户的地址
		$addr = DB::table('shop_address')->where('id',$request->input('address_id'))->first();
		
		//获取所有地区信息
		$area = DB::table('areas')->where('parentid',0)->orderBy('areaid','asc')->get();
		// dd($area);
		return view('home.address.edit',['addr'=>$addr,'area'=>$area]);
	}

	//执行修改
	public function postUpdate(Request $request)
	{
		$data = $request->except('_token','addr_id');
		// dd($data);
		//开启事务
		DB::beginTransaction();
		//检测是否设置默认地址
		$this->isdefault($request);

		//修改地址表
		$res = DB::table('shop_address')->where('id',$request->addr_id)->update($data);
		if($res){
			//如果没设置默认 直接结束
			DB::commit();
			return redirect('/home/address/index')->with('info','修改成功');
		}
		//上述修改成功条件不符合 则回滚
		DB::rollBack();
		return back()->with('error','修改错误');
		
	}

    //添加地址页面
    public function getAdd()
    {	
    	//获取省
    	$id = DB::table('areas')->where('parentid','0')->orderBy('areaid')->select('areaid')->get();
    	$area = getAreaNameById($id);
    	// dd($name);
    	return view('home.address.add',['area'=>$area]);
    }

    //获取市
	public function getAchieve(Request $request)
	{
		$pid = $request->input('pid');

    	$id = DB::table('areas')->where('parentid',$pid)->orderBy('areaid')->select('areaid')->get();
    	$area = getAreaNameById($id);
		return json_encode($area);	
	}

	//执行添加地址
	public function postDoadd(Request $request)
	{
		$addr = $request->except('_token');
		$addr['user_id'] = session('id'); 
		// dd($addr);
		DB::beginTransaction();
		//检测是否设置默认地址
		$this->isdefault($request);

		//插入表中
		$res = DB::table('shop_address')->insert($addr);
		if($res){
			// echo 111;
			DB::commit();
			return redirect('/home/address/index')->with('info','添加成功');
		}else{
			DB::rollback();
			return back()->with('error','添加失败');
		}
	}

	//删除地址
	public function getDelete(Request $request)
	{
		// dd($request->address_id);
		$res = DB::table('shop_address')->where('id',$request->address_id)->delete();
		if($res){
			return 1;
		}else{
			return 0;
		}
	}

	//检测默认地址 并置零
	private function isdefault($request)
	{
		if($request->isdefault == 1){
			//获取之前为默认地址的 id
			$prev_id = DB::table('shop_address')->where('user_id',session('id'))->where('isdefault',1)->select('id')->first()->id;
			// dd($prev_id);
			//将之前默认地址 1  改为  0
			if($prev_id){
				$defzero = DB::table('shop_address')->where('id',$prev_id)->update(['isdefault'=>0]);
			
				//如果修改失败
				if(!$defzero){
					DB::rollBack();
					return back()->with('error','修改错误');
				}
			}
		}
	}
}
